package top.chengdongqing.mishop.media.video

import org.springframework.cache.annotation.Cacheable
import org.springframework.data.domain.PageRequest
import org.springframework.data.domain.Sort
import org.springframework.stereotype.Service
import top.chengdongqing.mishop.entities.Video
import top.chengdongqing.mishop.repositories.VideoRepository

@Service
class VideoService(private val videoRepository: VideoRepository) {

    @Cacheable(value = ["media:videos"], key = "#limits?:'all'")
    fun findVideos(limits: Int?): List<VideoVO> {
        val sort = Sort.by(Video::createdAt.name).descending()
        val pageable = PageRequest.ofSize(limits ?: Int.MAX_VALUE).withSort(sort)
        val videos = videoRepository.findAllByEnabledTrue(pageable)
        return videos.map { VideoVO.of(it) }
    }
}